using System.Threading;
using System.Threading.Tasks;
using MediatR;
using UniversalAdmin.Application.Dtos.Log;
using UniversalAdmin.Application.Services.Interfaces;
using UniversalAdmin.Domain.Entities.App;

namespace UniversalAdmin.Application.Services.Handlers;

    public class UserUpdatedEventHandler : INotificationHandler<UserUpdatedEvent>
    {
        private readonly ILogService _logService;
        private readonly IUserContextService _userContextService;
        
        public UserUpdatedEventHandler(ILogService logService, IUserContextService userContextService)
        {
            _logService = logService;
            _userContextService = userContextService;
        }
        
        public async Task Handle(UserUpdatedEvent notification, CancellationToken cancellationToken)
        {
            var log = new CreateLogDto
            {
                UserId = _userContextService.GetCurrentUserId() ?? notification.User.Id, // 使用当前操作者ID
                Action = "Update",
                Resource = notification.User.Username,
                ResourceId = notification.User.Id
            };
            await _logService.AddAsync(log);
            
            Console.WriteLine($"用户更新事件触发，用户名：{notification.User.Username}");
        }
    }
